﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CM_GetContactedClientBySellman]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_CM_GetContactedClientBySellman];
GO
CREATE PROCEDURE [dbo].[sproc_CM_GetContactedClientBySellman]
@sellmanid int,
@begintime datetime,
@endtime datetime

AS
    IF(@sellmanid<>0)
begin
    SELECT * FROM UDS_CM_ClientInfo
    WHERE ID IN (
    SELECT UDS_CM_ClientContact.ClientID FROM UDS_CM_ClientContact
    WHERE MarketmanID=@sellmanid 
    AND ContactTime between @begintime and dateadd(day,1,@endtime)
    GROUP BY UDS_CM_ClientContact.ClientID)
end
    ELSE
begin
    SELECT * FROM UDS_CM_ClientInfo
    WHERE ID IN (
    SELECT UDS_CM_ClientContact.ClientID FROM UDS_CM_ClientContact
    WHERE 
    ContactTime between @begintime and dateadd(day,1,@endtime)
    GROUP BY UDS_CM_ClientContact.ClientID)
end